package com.bw.ivideoproject.push

import android.content.Intent
import android.os.Environment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bw.ivideoproject.R
import com.bw.ivideoproject.databinding.ActivityCameraVideoBinding
import com.bw.ivideoproject.databinding.ItemFileBinding
import com.bw.lib_mvvm.BaseActivity
import java.io.File
import java.text.DecimalFormat

class CameraVideoActivity : BaseActivity<ActivityCameraVideoBinding>() {

    private var list = mutableListOf<File>()
    private lateinit var fileAdapter: FileAdapter

    override fun initView() {
        val basePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).absolutePath
        val file = File(basePath)
        val listFiles = file.listFiles()
        //获取jpg文件
        for (listFile in listFiles) {
            if(listFile.name.toLowerCase().endsWith(".jpg")){
                list.add(listFile)
            }
        }
        //获取MP4文件
        for (listFile in listFiles) {
            if(listFile.name.toLowerCase().endsWith(".mp4")){
                list.add(listFile)
            }
        }
        fileAdapter = FileAdapter()
        binding.rvCameraVideo.apply {
            adapter = fileAdapter
            layoutManager = LinearLayoutManager(this@CameraVideoActivity)
        }
    }

    override fun initData() {
        fileAdapter.refresh(list)
    }

    override fun bindLayout(): Int {
        return R.layout.activity_camera_video
    }

    inner class FileAdapter : RecyclerView.Adapter<FileAdapter.FileViewHolder>() {

        private var data = mutableListOf<File>()

        inner class FileViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            val subBinding = ItemFileBinding.bind(itemView)
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FileViewHolder {
            val view =
                LayoutInflater.from(parent.context).inflate(R.layout.item_file, parent, false)
            return FileViewHolder(view)
        }

        override fun onBindViewHolder(holder: FileViewHolder, position: Int) {
            val file = data[position]
            holder.subBinding.adapterFileListName.text = file.name
            holder.subBinding.adapterFileListCreateSize.text = FormetFileSize(file.length())

            holder.subBinding.root.setOnClickListener {

                startActivity(Intent(this@CameraVideoActivity,PlayActivity::class.java).apply { putExtra("videoPath",file.absolutePath) })
            }
        }

        override fun getItemCount(): Int {
            return data.size
        }

        fun refresh(list: MutableList<File>) {
            data.clear()
            data.addAll(list)
            notifyDataSetChanged()
        }

        private fun FormetFileSize(length: Long): String {
            val df = DecimalFormat("#.00")
            var fileSizeString = ""
            fileSizeString = if (length < 1024) {
                df.format(length.toDouble()) + "B"
            } else if (length < 1048576) {
                df.format(length.toDouble() / 1024) + "K"
            } else if (length < 1073741824) {
                df.format(length.toDouble() / 1048576) + "M"
            } else {
                df.format(length.toDouble() / 1073741824) + "G"
            }
            return fileSizeString
        }

    }

}